<template>
  <div class="page-discount">
    <!-- 搜索栏 -->
    <div class="search-bar">
      <el-form
        ref="test-form"
        :inline="true"
        class="search-form"
        :model="searchForm"
        label-width="auto"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            :label="$t('groups.eventName') + ':'"
            class="search-form-item"
          >
            <el-input
              v-model="searchForm.discountName"
              :placeholder="$t('groups.eventName')"
            />
          </el-form-item>
          <el-form-item
            :label="$t('group.actStatus') + ':'"
            class="search-form-item"
          >
            <el-select
              v-model="searchForm.status"
              :placeholder="$t('group.actStatus')"
            >
              <el-option
                v-for="item in statusList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('station.search') }}
            </div>
            <div
              class="default-btn"
              @click="clearSearch"
            >
              {{ $t('temp.setNull') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <!-- 搜索栏end -->
    <!-- 表格主体 -->
    <div class="main-container">
      <div class="operation-bar">
        <div
          v-if="isAuth('seckill:seckill:save')"
          class="default-btn primary-btn"
          @click="onAddOrUpdate()"
        >
          {{ $t('crud.addTitle') }}
        </div>
      </div>
      <!-- 表格 -->
      <div class="table-con seckill-table">
        <el-table
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          stripe
          style="width: 100%"
        >
          <el-table-column
            align="left"
            prop="discountName"
            width="300"
            :label="$t('groups.eventName')"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.discountName }}</span>
            </template>
          </el-table-column>
          <!-- 移动端活动图 -->
          <el-table-column
            width="200"
            :label="$t('marketing.mobiltyDiagram')"
          >
            <template #default="scope">
              <div class="table-cell-image">
                <ImgShow :src="scope.row.mobilePic" />
              </div>
            </template>
          </el-table-column>

          <el-table-column
            width="150"
            align="left"
            prop="startTime"
            :label="$t('text.startTime')"
          />

          <el-table-column
            width="150"
            align="left"
            prop="endTime"
            :label="$t('text.endTime')"
          />

          <el-table-column
            align="center"
            prop="status"
            :label="$t('group.actStatus')"
          >
            <template #default="scope">
              <div class="tag-text">
                {{
                  [
                    $t('station.close'),
                    $t('station.open'),
                    $t('product.violation'),
                    $t('coupon.waitReview')
                  ][scope.row.status]
                }}
              </div>
            </template>
          </el-table-column>

          <el-table-column
            align="center"
            fixed="right"
            width="300"
            :label="$t('crud.menu')"
            class=""
          >
            <template #default="scope">
              <div class="text-btn-con">
                <div
                  v-if="isAuth('admin:discount:update')"
                  class="default-btn text-btn"
                  @click="onAddOrUpdate(scope.row.discountId)"
                >
                  {{ $t('groups.editEvent') }}
                </div>
                <div
                  v-if="isAuth('admin:discount:update')"
                  class="default-btn text-btn ;margin: 0px 20pxx"
                  @click="showProds(scope.row.discountId)"
                >
                  {{ $t('prodList.lookOver') }}
                </div>
                <div
                  v-if="isAuth('admin:discount:delete')"
                  class="default-btn text-btn"
                  :class="[scope.row.status === 2 ? 'disabled-btn' : '', 'default-btn text-btn']"
                  @click="onDelete(scope.row.discountId, scope.row.status)"
                >
                  {{ $t('text.delBtn') }}
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <el-pagination
        v-if="dataList.length"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @update:page-size="onPageSizeChange"
        @update:current-page="onPageChange"
      />
    </div>
    <!-- 表格主体end -->

    <!-- 弹窗, 新增 / 修改 -->
    <ViewProd
      v-if="viewProdVisible"
      ref="viewProdRef"
    />
  </div>
</template>

<script setup>
import ViewProd from '../components/view-or-select-prod.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { isAuth } from '@/utils/index'
import { useRouter } from 'vue-router'

const router = useRouter()
let tempSearchForm = null // 保存上次点击查询的请求条件
const dataList = ref([])
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
// 头部搜索表单
const searchForm = reactive({
  discountName: null,
  status: null
})

const statusList = [
  {
    value: 0,
    label: $t('station.close')
  },
  {
    value: 1,
    label: $t('station.open')
  }
]
const dataListSelections = ref([])
const viewProdVisible = ref(false)

onMounted(() => {
  getDataList()
})

// 获取数据列表
const getDataList = (pageParam, newData = false) => {
  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm))
  }
  http({
    url: http.adornUrl('/admin/discount/page'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        {
          current: pageParam == null ? page.currentPage : pageParam.currentPage,
          size: pageParam == null ? page.pageSize : pageParam.pageSize
        },
        tempSearchForm
      )
    )
  }).then(({ data }) => {
    dataList.value = data.records
    page.total = data.total
  })
}
// 新增or修改
const onAddOrUpdate = id => {
  router.push({ path: '/marketing/discount/add-or-update', query: { discountId: id } })
}

// 查看商品组件
const viewProdRef = ref(null)
const showProds = id => {
  viewProdVisible.value = true
  nextTick(() => {
    viewProdRef.value?.showProdInit(id, 1)
  })
}

// 删除
const onDelete = id => {
  const ids = id ? [id] : dataListSelections.value.map(item => {
    return item.discountId
  })
  ElMessageBox.confirm(
    `${$t('sys.makeSure')}${$t('text.delBtn')}${$t('text.menu')}?`,
    $t('text.tips'),
    {
      confirmButtonText: $t('crud.filter.submitBtn'),
      cancelButtonText: $t('crud.filter.cancelBtn'),
      type: 'warning'
    }
  ).then(() => {
    http({
      url: http.adornUrl('/admin/discount/' + id),
      method: 'delete',
      data: http.adornData({}, false)
    }).then(() => {
      page.total = page.total - ids.length
      ElMessage({
        message: $t('publics.operation'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          getDataList(page)
        }
      })
    })
  })
}
// 条件查询
const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}
const clearSearch = () => {
  searchForm.discountName = null
  searchForm.status = null
}
// 每页数量变更
const onPageSizeChange = val => {
  page.pageSize = val
  getDataList()
}
// 页数变更
const onPageChange = val => {
  page.currentPage = val
  getDataList()
}
</script>
